package niumao.minecraft.uhc;

import niumao.minecraft.uhc.indicator.ScoreBoardInfo;
import niumao.minecraft.uhc.recipes.UHCItem;
import org.bukkit.*;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.player.*;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.scheduler.BukkitRunnable;

import java.util.Objects;
import java.util.UUID;

public class DefaultSetting implements Listener {
    private final static UUID villagerUUID = UUID.fromString("8058fbf3-391b-4dce-9afc-0cbe99e57b5f");       //开始游戏的NPC的id
    private final static UUID armorStandUUID = UUID.fromString("14448428-d306-489a-9fb7-1ab4866d46fe");     //语言文字显示的id
    private static final Villager villager = (Villager) Bukkit.getEntity(villagerUUID);
    private static final ArmorStand armorstand = (ArmorStand) Bukkit.getEntity(armorStandUUID);
    private static boolean canSwitchLang = true;    //防止多次点击导致服务器卡顿

    //更改大厅开始游戏npc的名字
    public static void setNPCName(String string) {
        assert villager != null;
        villager.setCustomName(string);
    }

    public static void setArmorStandName(String string) {
        assert armorstand != null;
        armorstand.setCustomName(string);
    }

    //设置玩家的初始血量情况,设置sidebar的计分板显示
    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        Player player = event.getPlayer();
        if (player.getWorld().getName().equalsIgnoreCase("world")) {
            player.setHealthScale(40D);
            Objects.requireNonNull(player.getAttribute(Attribute.GENERIC_MAX_HEALTH)).setBaseValue(40D);
            player.setHealth(40D);
            player.setFoodLevel(20);
            player.setGameMode(GameMode.ADVENTURE);
            player.setInvulnerable(true);
            ScoreBoardInfo.Flag = true;
            ScoreBoardInfo.initBase(player);

           /* ArmorStand armorStand = (ArmorStand) player.getWorld().spawnEntity(new Location(player.getWorld(), 3.5, 5, 0.5), EntityType.ARMOR_STAND);
            armorStand.setVisible(false);
            armorStand.setCollidable(true);
            armorStand.setGravity(false);
            armorStand.setMarker(true);
            armorStand.setCustomNameVisible(true);
            armorStand.setInvulnerable(true);
            Villager villager = (Villager) player.getWorld().spawnEntity(new org.bukkit.Location(player.getWorld(), 0.5, 4, 0.5), EntityType.VILLAGER);
            villager.setAI(false);   //可以防止被玩家推动
            villager.setCollidable(true);
            villager.setInvulnerable(true);
            villager.setCustomNameVisible(true);

            Bukkit.broadcastMessage(villager.getUniqueId().toString());
            Bukkit.broadcastMessage(armorStand.getUniqueId().toString());*/
        }else if (player.getWorld().getName().equalsIgnoreCase("uhc")
                ||player.getWorld().getName().equalsIgnoreCase("world_nether")){
            ScoreBoardInfo.Flag = false;
            ScoreBoardInfo.initUHC(player);
        }
    }

    //玩家在大厅世界不能进行的操作
    @EventHandler
    public void onPlayerClickInventory(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();
        if (player.isOp() && player.getGameMode().equals(GameMode.CREATIVE)) return;
        if (player.getWorld().getName().equalsIgnoreCase("world"))
            event.setCancelled(true);
    }

    //玩家在大厅世界不能进行的操作
    @EventHandler
    public void onPlayerDropItem(PlayerDropItemEvent event) {
        if (event.getPlayer().isOp() && event.getPlayer().getGameMode() == GameMode.CREATIVE) return;
        if (event.getPlayer().getWorld().getName().equalsIgnoreCase("world"))
            event.setCancelled(true);
    }

    //玩家在大厅世界的交互操作
    @EventHandler
    public void onPlayerInteractNPC(PlayerInteractEntityEvent event) {
        Entity entity = event.getRightClicked();
        if (!entity.getWorld().getName().equalsIgnoreCase("world")) return;
        if (event.getHand() == EquipmentSlot.HAND) {  //防止trigger twice
            Player player = event.getPlayer();
            if (entity.getName().equalsIgnoreCase(Main.getInstance().getConfig().getString("ServerInfo." + Main.getLang() + ".UHC"))) {
                if (!player.isOp()) {
                    player.sendMessage("You are not Op, can't start this game!");
                    return;
                }
                if (Bukkit.getOnlinePlayers().size() == 1) {
                    event.getPlayer().sendMessage("At least 2 players can start game!");
                    return;
                }
                if (UHCGame.canStart) UHCGame.start();
            } else if (entity.getName().equalsIgnoreCase("§a幸运方块") && entity.getType().equals(EntityType.PANDA)) {
                if (!player.isOp()) {
                    player.sendMessage("You are not Op, can't start this game!");
                    return;
                }
                if (Bukkit.getOnlinePlayers().size() == 1) {
                    event.getPlayer().sendMessage("At least 2 players can start game!");
                    return;
                }
                LuckyBlockGame.start();
            }
        }
    }

    //点击按钮切换语言
    @EventHandler
    public void onPlayerInteractButton(PlayerInteractEvent event) {
        if (!canSwitchLang) return;
        if (event.getHand() == EquipmentSlot.HAND) {
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                Player player = event.getPlayer();
                if (event.getClickedBlock().getType() == Material.STONE_BUTTON
                        && event.getClickedBlock().getLocation().equals(new Location(player.getWorld(), 3, 5, 0))
                        && !event.getClickedBlock().isBlockPowered()) {
                    if (!player.isOp()) {
                        player.sendMessage("You are not Op, can't select language!");
                        return;
                    }
                    if (Main.getLang().equalsIgnoreCase("en_us")) Main.setLang("zh_cn");
                    else Main.setLang("en_us");
                    new UHCItem().setAllItems();
                    new UHCItem().setRecipe();
                    DefaultSetting.setNPCName(Main.getInstance().getConfig().getString("ServerInfo." + Main.getLang() + ".UHC"));
                    DefaultSetting.setArmorStandName(Main.getInstance().getConfig().getString("ServerInfo." + Main.getLang() + ".LanguageOptions"));
                    canSwitchLang = false;
                    new BukkitRunnable() {
                        @Override
                        public void run() {
                            canSwitchLang = true;
                        }
                    }.runTaskLater(Main.getInstance(), 20);
                }
            }
        }
    }
}